<template>
  <div class="app-content-wrap" :style="'minHeight:' + gHeight + 'px;'">
    <div class="g-content-title">线上发布</div>
    <div style="margin:10px 30px;">
      <el-tabs v-model="tabName" @tab-click="tabChangeHandler">
        <el-tab-pane label="公告管理" name="first">
          <!-- 搜索框 -->
          <el-form :model="userForm" label-width="70px" label-position="left" size="small">
            <el-row :gutter="15">
              <el-col :span="5">
                <el-form-item label="发布单位">
                  <el-input v-model="userForm.sponsor" placeholder="发布单位"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label="公告主题">
                  <el-input v-model="userForm.title" placeholder="公告主题"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="11">
                <el-form-item label="所属小区">
                  <AreaSelect :selectModel="userForm.deptId" :areaIds="areaIds" @onAreaChange="onAreaChange" hideThird="true">
                  </AreaSelect>
                </el-form-item>
              </el-col>
              <el-col :span="3">
                <el-form-item>
                  <el-button type="primary" size="small" icon="el-icon-search" @click="getFromFilter">搜 索</el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <!-- 列表 -->
          <div style="margin:10px -10px">
            <el-table :data="userListTable" border style="width: 100%">
              <el-table-column prop="title" label="公告主题" min-width="100" align="center"></el-table-column>
              <el-table-column prop="sponsor" label="发布单位" min-width="120" align="center"></el-table-column>
              <el-table-column prop="createTime" label="发布时间" min-width="140" align="center"></el-table-column>
              <el-table-column prop="status" label="状态" min-width="140" align="center" :formatter="noticeStatuToText"></el-table-column>
              <el-table-column label="操作" width="190" align="center">
                <template slot-scope="scope">
                  <el-button type="text" @click="noticeDetail(scope.row, 1)">详情</el-button>
                  <el-button type="text" @click="noticeOffline(scope.row, 1)">下线</el-button>
                  <!-- <el-button type="text" @click="userModify(scope.row)">编辑</el-button> -->
                  <el-button type="text" @click="noticeDelete(scope.row, 1)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-pagination background @current-change="handleCurrentChange" :current-page.sync="pagination.currentPage" :page-size="10" layout="total, prev, pager, next,jumper" :total="pagination.total" class="el-page-body"></el-pagination>
          </div>
        </el-tab-pane>
        <el-tab-pane label="活动管理" name="second">
          <!-- 搜索框 -->
          <el-form :model="userForm" label-width="70px" label-position="left" size="small">
            <el-row :gutter="15">
              <el-col :span="5">
                <el-form-item label="发布单位">
                  <el-input v-model="userForm.sponsor" placeholder="发布单位"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label="活动主题">
                  <el-input v-model="userForm.title" placeholder="活动主题"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="11">
                <el-form-item label="所属小区">
                  <AreaSelect :selectModel="userForm.deptId" :areaIds="areaIds" @onAreaChange="onAreaChange" hideThird="true">
                  </AreaSelect>
                </el-form-item>
              </el-col>
              <el-col :span="3">
                <el-form-item>
                  <el-button type="primary" size="small" icon="el-icon-search" @click="getFromFilter">搜 索</el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <!-- 列表 -->
          <div style="margin:10px -10px">
            <el-table :data="userListTable" border style="width: 100%">
              <el-table-column prop="title" label="活动主题" min-width="100" align="center"></el-table-column>
              <el-table-column prop="sponsor" label="举办单位" min-width="140" align="center"></el-table-column>
              <el-table-column prop="createTime" label="活动时间" min-width="140" align="center"></el-table-column>
              <el-table-column prop="applyNum" label="报名人数" min-width="140" align="center"></el-table-column>
              <el-table-column prop="status" label="状态" min-width="140" align="center" :formatter="activityStatuToText"></el-table-column>
              <el-table-column label="操作" width="190" align="center">
                <template slot-scope="scope">
                  <el-button type="text" @click="noticeDetail(scope.row, 2)">详情</el-button>
                  <el-button type="text" @click="noticeOffline(scope.row, 2)">下线</el-button>
                  <!-- <el-button type="text" @click="userModify(scope.row)">编辑</el-button> -->
                  <el-button type="text" @click="noticeDelete(scope.row, 2)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-pagination background @current-change="handleCurrentChange" :current-page.sync="pagination.currentPage" :page-size="10" layout="total, prev, pager, next,jumper" :total="pagination.total" class="el-page-body"></el-pagination>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>
<script>
import AreaSelect from '../../components/common/select/area.vue'

export default {
  name: 'community',
  components: {
    AreaSelect
  },
  data () {
    return {
      gHeight: this.$store.state.gHeight,
      tabName: 'first',
      areaIds: {
        areaId1: '',
        areaId2: '',
        areaId3: ''
      },
      userForm: {
        sponsor: '', // 发布单位
        title: '', // 主题
        deptId: '' // 小区
      },
      userListTable: [],
      pagination: {
        currentPage: null,
        total: null
      }
    }
  },
  created () {
  },
  methods: {
    // 公告状态转文本
    noticeStatuToText (row) {
      return this.$base.getNameByConst('select', 'noticeStatu', row.status)
    },
    // 活动状态转文本
    activityStatuToText (row) {
      return this.$base.getNameByConst('select', 'activityStatu', row.status)
    },
    // tab toggle
    tabChangeHandler (tabV) {
      this.userForm.title = ''
      this.userForm.deptId = ''
      this.userForm.sponsor = ''
      this.pagination.currentPage = 1
      this.areaIds.areaId1 = ''
      this.areaIds.areaId2 = ''
      this.getList()
    },
    noticeDetail: function (row, type) {
      if (type === 1) {
        this.$router.push({
          name: 'noticeDetail',
          params: {
            id: row.id
          }
        })
      } else {
        this.$router.push({
          name: 'activityDetail',
          params: {
            id: row.id
          }
        })
      }
    },
    noticeOffline (row, type) {
      let id = row.id
      this.$confirm('确定下线?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleOffline(id, type)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    noticeDelete (row, type) {
      let id = row.id
      this.$confirm('确定删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(id, type)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleOffline (id, type) {
      let url = ''
      if (type === 1) {
        url = this.$base.getUrl('frontBiznoticeinfoOffline')
      } else {
        url = this.$base.getUrl('frontBizactivityinfoOffline')
      }
      this.$store.dispatch('postForm', {
        isJson: true,
        url: url,
        data: {
          id: id
        }
      }).then((res) => {
        if (res.code === 0) {
          this.$message({
            type: 'success',
            message: '下线成功!'
          })
          this.getList()
        }
      })
    },
    handleDelete (id, type) {
      let url = ''
      if (type === 1) {
        url = this.$base.getUrl('frontBiznoticeinfoDelete')
      } else {
        url = this.$base.getUrl('frontBizactivityinfoDelete')
      }
      this.$store.dispatch('postForm', {
        isJson: true,
        url: url,
        data: {
          id: id
        }
      }).then((res) => {
        if (res.code === 0) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList()
        }
      })
    },
    getFromFilter () {
      this.pagination.currentPage = 1
      this.getList()
    },
    handleCurrentChange (currPage) {
      this.pagination.page = currPage
      this.getList()
    },
    getList () {
      let url = ''
      if (this.tabName === 'first') {
        url = this.$base.getUrl('frontBiznoticeinfoList')
      } else {
        url = this.$base.getUrl('frontBizactivityinfoList')
      }
      let params = {
        isJson: true,
        url: url,
        data: {
          sponsor: this.userForm.sponsor,
          title: this.userForm.title,
          deptId: this.userForm.deptId,
          page: this.pagination.currentPage,
          limit: 10
        }
      }
      this.$store.dispatch('postForm', params).then((res) => {
        let { result } = res
        this.userListTable = result.list
        this.pagination.currentPage = result.currPage
        this.pagination.total = result.totalCount
      })
    },
    // area组件change时
    onAreaChange (val) {
      this.userForm.deptId = val
    }
  },
  mounted () {
    this.getList()
  }
}
</script>
<style lang='less' scoped>
.el-table {
  width: auto !important;
  text-align: center;
  margin: 10px;
}
/deep/ .el-table th {
  text-align: center;
}
/deep/ .el-tabs__item {
  color: #fff;
}
</style>
